<?php

namespace App\Console\Commands\CallingCards;

use App\Models\FeishuCallingCard\FeishuCallingCard;
use App\Service\CallingCards\CardService;
use Illuminate\Console\Command;

class RefreshWechatQrcode extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'calling_cards:refresh_wechat_qrcode {from_id} {to_id}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '飞书员工加盟名片 刷新员工微信小程序推广码';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        FeishuCallingCard::query()
            ->whereBetween('id', [(int)$this->argument('from_id'), (int)$this->argument('to_id')])
            ->chunk(100, function ($cards) {
                foreach ($cards as $card) {
                    $this->info("id:{$card->id}, name:{$card->name},更新中...");
                    $card->refreshWechatQrcode();
                    $card->save();
                    // 更新为空的微信分享图
                    if ($card->wxShareImg == '') {
                        CardService::updateWxShareImg($card);
                    }
                }
            });
        return 0;
    }
}
